package com.pashanhoo.netty.nio.channel;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author 胡庆安
 * @version V1.0
 * @Package com.pashanhoo.netty.nio.channel
 * @date 2020/11/4 17:11
 * @Copyright © 2010-2020 爬山虎科技（扬州）股份有限公司
 */
public class NIOFileChannel02 {
    public static void main(String[] args) throws IOException {
        File file = new File("d:\\file.txt");
        FileInputStream fileInputStream = new FileInputStream(file);
        //通过文件输出流获取对应的文件channel
        FileChannel channel = fileInputStream.getChannel();
        ByteBuffer byteBuffer = ByteBuffer.allocate((int)file.length());
        channel.read(byteBuffer);

        //将字节转成字符串
        System.out.println(new String(byteBuffer.array()));
        fileInputStream.close();
    }
}
